//
//  BillViewController.m
//  cm
//
//  Created by suyibaishan on 2017/11/6.
//  Copyright © 2017年 feihu. All rights reserved.
//

#import "BillViewController.h"
#import "BillListCell.h"
#import "BillDetailCell.h"

@interface BillViewController () <UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableView;
    NSMutableArray *dataSource;
    UIView *headView;
    UILabel *nameLabel;
    UIButton *invertedBtn;
    BOOL isList;
    NSInteger type;
    UIView *commissionView;
    UIView *withdrawView;
     NSInteger page;
}
@end

@implementation BillViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createBillView];
    
    [self reloadData];
}

-(void)reloadData
{
    [[ToolNet sharedManager] getExchangeRecord:[mUserDefaults objectForKey:@"userCode"] accessToken:[mUserDefaults objectForKey:@"accessToken"] type:type pageNum:1 pageSize:16 url:MExchangeRecord completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            [dataSource removeAllObjects];
            [dataSource addObjectsFromArray:responseJson[@"data"][@"list"]];
            [_tableView reloadData];
            page = 2;
        }
        else {
            [self showMessage:responseJson[@"msg"]];
        }
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    }];
}

-(void)loadMoreData
{
    [[ToolNet sharedManager] getExchangeRecord:[mUserDefaults objectForKey:@"userCode"] accessToken:[mUserDefaults objectForKey:@"accessToken"] type:type pageNum:page pageSize:16 url:MExchangeRecord completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            if ([responseJson[@"data"][@"pages"] integerValue] >= page) {
                [dataSource addObjectsFromArray:responseJson[@"data"][@"list"]];
                [_tableView.mj_footer endRefreshing];
                page++;
            }
            else{
                [_tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [_tableView reloadData];
        }
        else {
            [self showMessage:responseJson[@"msg"]];
            [_tableView.mj_footer endRefreshing];
        }
    }];
}

-(void)createBillView
{
    self.titleLabel.text = @"我的账单";
    self.backButton.hidden = NO;
    
    [self createHeadView];
    
    type = 1;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.baseBarHeight, kScreenWidth, kScreenHeight - self.baseBarHeight - 10) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _tableView.tableHeaderView = headView;
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self reloadData];
    }];
    _tableView.mj_header.automaticallyChangeAlpha = YES;
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];

    dataSource = [NSMutableArray array];
}

-(void)createHeadView
{
    headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 55+30)];
    
    nameLabel = [[UILabel alloc] init];
    nameLabel.font = [UIFont systemFontOfSize:14];
    nameLabel.text = @"佣金明细";
    nameLabel.textColor = [UIColor blackColor];
    [headView addSubview:nameLabel];
    nameLabel.sd_layout
    .topSpaceToView(headView, 20)
    .leftSpaceToView(headView, 15)
    .widthIs(80)
    .heightIs(35);
    
    invertedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [invertedBtn setTitle:@"切换提现列表" forState:UIControlStateNormal];
    [invertedBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    invertedBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    UIImage *invertedImg = [[UIImage imageNamed:@"system_mini"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [invertedBtn setImage:invertedImg forState:UIControlStateNormal];
    [invertedBtn addTarget:self action:@selector(invertedBtnHanlder) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:invertedBtn];
    invertedBtn.sd_layout
    .topSpaceToView(headView, 20)
    .rightSpaceToView(headView, 10)
    .widthIs(100)
    .heightIs(35);
    
    commissionView = [[UIView alloc] init];
    [headView addSubview:commissionView];
    commissionView.sd_layout
    .bottomSpaceToView(headView, 0)
    .leftSpaceToView(headView, 15)
    .widthIs(kScreenWidth - 30)
    .heightIs(30);
    
    for (NSInteger i = 0; i < 3; i++) {
        UILabel *label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:14];
        label.text = i == 0?@"日期":i == 1?@"类型":@"金额(元)";
        label.textColor = [UIColor blackColor];
        [commissionView addSubview:label];
        label.sd_layout
        .topSpaceToView(commissionView, 0)
        .leftSpaceToView(commissionView, i*(kScreenWidth-30)/3)
        .widthIs((kScreenWidth-30)/3)
        .heightIs(30);
    }
    
    withdrawView = [[UIView alloc] init];
    withdrawView.hidden = YES;
    [headView addSubview:withdrawView];
    withdrawView.sd_layout
    .bottomSpaceToView(headView, 0)
    .leftSpaceToView(headView, 15)
    .widthIs(kScreenWidth - 30)
    .heightIs(30);
    
    for (NSInteger j = 0; j < 4; j++) {
        UILabel *label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:14];
        label.text = j == 0?@"日期":j == 1?@"类型":j == 2?@"金额(元)":@"状态";
        label.textColor = [UIColor blackColor];
        [withdrawView addSubview:label];
        label.sd_layout
        .bottomSpaceToView(withdrawView, 0)
        .leftSpaceToView(withdrawView, j*(kScreenWidth-30)/4)
        .widthIs((kScreenWidth-30)/4)
        .heightIs(30);
    }
}

-(void)invertedBtnHanlder
{
    if (isList) {
        nameLabel.text = @"佣金明细";
        [invertedBtn setTitle:@"切换提现列表" forState:UIControlStateNormal];
        type = 1;
        isList = NO;
        commissionView.hidden = NO;
        withdrawView.hidden = YES;
    }
    else {
        nameLabel.text = @"提现明细";
        [invertedBtn setTitle:@"切换佣金明细" forState:UIControlStateNormal];
        type = 2;
        isList = YES;
        commissionView.hidden = YES;
        withdrawView.hidden = NO;
    }
    [self reloadData];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (isList) {
        static NSString *cellIdentifier = @"cellIdentifier";
        BillDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[BillDetailCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = [[BillDetailModel alloc] initWithDictionary:dataSource[indexPath.row]];
        
        return cell;
    }
    static NSString *billListCell = @"billListCell";
    BillListCell *cell = [tableView dequeueReusableCellWithIdentifier:billListCell];
    if (!cell) {
        cell = [[BillListCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:billListCell];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [[BillListModel alloc] initWithDictionary:dataSource[indexPath.row]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
